low level file io

function proto type example
open/create int open(const char *path, int oflag, [, mode_t mode]);
create int create(const char *path, mode_t mode);
close int close(int fd);
read data ssize_t read(int fd, void *buf, size_t count);
write data ssize_t write(int fd, const void *buf, size_t count);
change file offset off_t lseek(int fd, off_t offset, int whence);
duplicate file descriptor int dup(int oldfd);
int dup2(int oldfd, int newfd);
delete file int unlink(const char *path);
int remove(const char *path);
disk synchronization fsync



high level file io

function proto type example
open/create FILE *fopen(const char *filename, const char *mode);
close int fclose(FILE *stream);
read characters int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
int getw(FILE *stream);
write characters int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
int putw(w, FILE *stream);
read string int gets(char *s);
int fgets(char *s, int n, FILE *stream);
write string int puts(const char *s);
int fputs(const char *s, FILE *stream);
read buffer size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
write buffer size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
formatted read int puts(const char *s);
int fputs(const char *s, FILE *stream);
formatted write int puts(const char *s);
int fputs(const char *s, FILE *stream);
file offset int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
int fsetpost(FILE *stream, const fpos_t *pos);
int fgetpos(FILE *stream, fpos_t *pos);
disk synchronization int fflush(FILE *stream);



low level file io vs. high level file io

low level high level
file descriptor int fd FILE *fd
file descriptor fast
bytewise
accessible to special file
easy
bufferwise access
format
io sync



file descriptor to/from file pointer

proto type example
file descriptor to file pointer FILE *fdopen(int fd, const char *mode);
file pointer to file descriptor int fileno(FILE *stream);



temporary file

function proto type example
create filename char *tmpnam(char *s);
char *tempnam(const char *dir, const char *pfx);
char *mktemp(char *template);
create pointer FILE *tmpfile();